﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class SysModule
    {
        public int Add(Chain.Model.SysModule model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into SysModule(");
            builder.Append("ModuleCaption,ModuleLink,ModuleParentID,ModuleOrder,ModuleVisible,ModuleIcoPath,ModuleRemark)");
            builder.Append(" values (");
            builder.Append("@ModuleCaption,@ModuleLink,@ModuleParentID,@ModuleOrder,@ModuleVisible,@ModuleIcoPath,@ModuleRemark)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ModuleCaption", SqlDbType.NVarChar, 200), new SqlParameter("@ModuleLink", SqlDbType.VarChar, 500), new SqlParameter("@ModuleParentID", SqlDbType.Int, 4), new SqlParameter("@ModuleOrder", SqlDbType.Int, 4), new SqlParameter("@ModuleVisible", SqlDbType.Bit, 1), new SqlParameter("@ModuleIcoPath", SqlDbType.VarChar, 50), new SqlParameter("@ModuleRemark", SqlDbType.NVarChar, 50) };
            cmdParms[0].Value = model.ModuleCaption;
            cmdParms[1].Value = model.ModuleLink;
            cmdParms[2].Value = model.ModuleParentID;
            cmdParms[3].Value = model.ModuleOrder;
            cmdParms[4].Value = model.ModuleVisible;
            cmdParms[5].Value = model.ModuleIcoPath;
            cmdParms[6].Value = model.ModuleRemark;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public Chain.Model.SysModule DataRowToModel(DataRow row)
        {
            Chain.Model.SysModule module = new Chain.Model.SysModule();
            if (row != null)
            {
                if ((row["ModuleID"] != null) && (row["ModuleID"].ToString() != ""))
                {
                    module.ModuleID = int.Parse(row["ModuleID"].ToString());
                }
                if (row["ModuleCaption"] != null)
                {
                    module.ModuleCaption = row["ModuleCaption"].ToString();
                }
                if (row["ModuleLink"] != null)
                {
                    module.ModuleLink = row["ModuleLink"].ToString();
                }
                if ((row["ModuleParentID"] != null) && (row["ModuleParentID"].ToString() != ""))
                {
                    module.ModuleParentID = int.Parse(row["ModuleParentID"].ToString());
                }
                if ((row["ModuleOrder"] != null) && (row["ModuleOrder"].ToString() != ""))
                {
                    module.ModuleOrder = int.Parse(row["ModuleOrder"].ToString());
                }
                if ((row["ModuleVisible"] != null) && (row["ModuleVisible"].ToString() != ""))
                {
                    if ((row["ModuleVisible"].ToString() == "1") || (row["ModuleVisible"].ToString().ToLower() == "true"))
                    {
                        module.ModuleVisible = true;
                    }
                    else
                    {
                        module.ModuleVisible = false;
                    }
                }
                if (row["ModuleIcoPath"] != null)
                {
                    module.ModuleIcoPath = row["ModuleIcoPath"].ToString();
                }
                if (row["ModuleRemark"] != null)
                {
                    module.ModuleRemark = row["ModuleRemark"].ToString();
                }
            }
            return module;
        }

        public bool Delete(int ModuleID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysModule ");
            builder.Append(" where ModuleID=@ModuleID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ModuleID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ModuleID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string ModuleIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysModule ");
            builder.Append(" where ModuleID in (" + ModuleIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int ModuleID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from SysModule");
            builder.Append(" where ModuleID=@ModuleID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ModuleID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ModuleID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataTable GetList(int moduleParentID)
        {
            string sQLString = "select * from SysModule where ModuleParentID=@ModuleParentID order by ModuleOrder,ModuleID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ModuleParentID", SqlDbType.Int, 4) };
            cmdParms[0].Value = moduleParentID;
            return DbHelperSQL.Query(sQLString, cmdParms).Tables[0];
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ModuleID,ModuleCaption,ModuleLink,ModuleParentID,ModuleOrder,ModuleVisible,ModuleIcoPath,ModuleRemark ");
            builder.Append(" FROM SysModule ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" ModuleID,ModuleCaption,ModuleLink,ModuleParentID,ModuleOrder,ModuleVisible,ModuleIcoPath,ModuleRemark ");
            builder.Append(" FROM SysModule ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int PageSize, int PageIndex, string[] strWhere, out int resCount)
        {
            string tableName = "SysModule";
            string[] column = new string[] { "*" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "ID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.ModuleID desc");
            }
            builder.Append(")AS Row, T.*  from SysModule T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ModuleID", "SysModule");
        }

        public Chain.Model.SysModule GetModel(int ModuleID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 ModuleID,ModuleCaption,ModuleLink,ModuleParentID,ModuleOrder,ModuleVisible,ModuleIcoPath,ModuleRemark from SysModule ");
            builder.Append(" where ModuleID=@ModuleID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ModuleID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ModuleID;
            new Chain.Model.SysModule();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                return this.DataRowToModel(set.Tables[0].Rows[0]);
            }
            return null;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM SysModule ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.SysModule model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update SysModule set ");
            builder.Append("ModuleCaption=@ModuleCaption,");
            builder.Append("ModuleLink=@ModuleLink,");
            builder.Append("ModuleParentID=@ModuleParentID,");
            builder.Append("ModuleOrder=@ModuleOrder,");
            builder.Append("ModuleVisible=@ModuleVisible,");
            builder.Append("ModuleIcoPath=@ModuleIcoPath,");
            builder.Append("ModuleRemark=@ModuleRemark");
            builder.Append(" where ModuleID=@ModuleID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ModuleCaption", SqlDbType.NVarChar, 200), new SqlParameter("@ModuleLink", SqlDbType.VarChar, 500), new SqlParameter("@ModuleParentID", SqlDbType.Int, 4), new SqlParameter("@ModuleOrder", SqlDbType.Int, 4), new SqlParameter("@ModuleVisible", SqlDbType.Bit, 1), new SqlParameter("@ModuleIcoPath", SqlDbType.VarChar, 50), new SqlParameter("@ModuleRemark", SqlDbType.NVarChar, 50), new SqlParameter("@ModuleID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.ModuleCaption;
            cmdParms[1].Value = model.ModuleLink;
            cmdParms[2].Value = model.ModuleParentID;
            cmdParms[3].Value = model.ModuleOrder;
            cmdParms[4].Value = model.ModuleVisible;
            cmdParms[5].Value = model.ModuleIcoPath;
            cmdParms[6].Value = model.ModuleRemark;
            cmdParms[7].Value = model.ModuleID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public int UpdateIsDataInit(int intType)
        {
            return DbHelperSQL.ExecuteSql(" update SysModule set ModuleVisible=" + intType + " where ModuleID=83");
        }
    }
}

